package com.asianfo.dao;

import com.mysql.jdbc.MiniAdmin;

public class StringUtil {

	public static String escape(String s) {
		int length = s.length();
		int newLength = length;
		for (int i = 0; i < length; ++i) {
			char c = s.charAt(i);
			switch (c) {
			case '\0':
			case '"':
			case '\'':
			case '\\':
				++newLength;
			}

		}

		if (length == newLength) {
			return s;
		}
		StringBuilder sb = new StringBuilder(newLength);
		for (int i = 0; i < length; ++i) {
			char c = s.charAt(i);
			switch (c) {
			case '\\':
				sb.append("\\\\");

				break;
			case '"':
				sb.append("\\\"");

				break;
			case '\'':
				sb.append("\\'");

				break;
			case '\0':
				sb.append("\\0");

				break;
			default:
				sb.append(c);
			}
		}

		return sb.toString();
	}
	
	
	public static void main(String[] args) {
		System.out.println(escape("\""));
	}
}
